package com.lxt.controller;

import com.lxt.common.HttpResult;
import com.lxt.common.util.JsonUtils;
import com.lxt.common.util.RedisOperator;
import com.lxt.enums.YesOrNo;
import com.lxt.pojo.Carousel;
import com.lxt.pojo.Category;
import com.lxt.pojo.VO.CategoryVO;
import com.lxt.pojo.VO.NewItemsVO;
import com.lxt.service.CarouselService;
import com.lxt.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@Api(value = "首页", tags = {"首页展示的相关接口"})
@RestController
@RequestMapping("index")
public class IndexController {

    final static Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private CarouselService carouselService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表", httpMethod = "GET")
    @GetMapping("/carousel")
    public HttpResult carousel() {
        List<Carousel> list = new ArrayList<>();
        String carouselStr = redisOperator.get("carousel");
        //如果redis缓存中的carousel为空则去查询数据库中的数据赋值
        if (StringUtils.isBlank(carouselStr)) {
            list = carouselService.queryAll(YesOrNo.YES.type);
            redisOperator.set("carousel", JsonUtils.objectToJson(list));
            logger.info("首页轮播图列表：" + JsonUtils.objectToJson(list));
            //如果redis中的carousel有值，则直接将其赋值给list
        } else {
            list = JsonUtils.jsonToList(carouselStr, Carousel.class);
        }
        return HttpResult.ok(list);

        /**
         * 1.后台运营系统，一旦广告(轮播图)发生更改，就可以删除缓存，然后重置
         * 2.定时重置，例如每天凌晨三点重置
         * 3.每个轮播图都可能是一个广告，每个广告都会有一个过期时间，过期了再重置
         */
    }

    /**
     * 首页分类展示需求：
     * 1.第一次刷新主页查询大分类，渲染展示到首页
     * 2.如果鼠标移动到大分类，则加载其子分类的内容，如果已经存在子分类，则不需要加载(懒加载)
     */
    @ApiOperation(value = "获取商品分类(一级分类)", notes = "获取商品分类(一级分类)", httpMethod = "GET")
    @GetMapping("/cats")
    public HttpResult cats() {
        List<Category> list = new ArrayList<>();
        String catsStr = redisOperator.get("cats");
        if (StringUtils.isBlank(catsStr)) {
            list = categoryService.queryAllRootLevelCat();
            redisOperator.set("cats", JsonUtils.objectToJson(list));
            logger.info("首页一级分类列表：" + JsonUtils.objectToJson(list));
        } else {
            list = JsonUtils.jsonToList(catsStr, Category.class);
        }
        return HttpResult.ok(list);
    }

    @ApiOperation(value = "获取商品子分类", notes = "获取商品子分类", httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public HttpResult subCat(
            @ApiParam(name = "rootCatId", value = "一级分类Id", required = true)
            @PathVariable Integer rootCatId) {
        if (rootCatId == null) {
            return HttpResult.errorMsg("分类不存在");
        }
        List<CategoryVO> list = new ArrayList<>();
        String subCatStr = redisOperator.get("subCatStr" + "_" + rootCatId);
        if (StringUtils.isBlank(subCatStr)) {
            list = categoryService.getSubCatList(rootCatId);
            redisOperator.set("subCatStr" + "_" + rootCatId, JsonUtils.objectToJson(list));
            logger.info("获取商品子分类列表：" + JsonUtils.objectToJson(list));
        } else {
            list = JsonUtils.jsonToList(subCatStr, CategoryVO.class);
        }
        return HttpResult.ok(list);
    }

    @ApiOperation(value = "查询每个一级分类下的最新6条商品数据", notes = "查询每个一级分类下的最新6条商品数据", httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public HttpResult getSixNewItemsLazy(
            @ApiParam(name = "rootCatId", value = "一级分类Id", required = true)
            @PathVariable Integer rootCatId) {
        if (rootCatId == null) {
            return HttpResult.errorMsg("分类不存在");
        }
        List<NewItemsVO> list = categoryService.getSixNewItemsLazy(rootCatId);
        logger.info("查询每个一级分类下的最新6条商品数据：" + JsonUtils.objectToJson(list));
        return HttpResult.ok(list);
    }
}
